package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.ICartService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/cart")
@CrossOrigin("*")
public class CartController {

    @Autowired
    private ICartService iCartService;

    Date date=new Date();

    //id,goodId,uId,num
    @RequestMapping("/insert/{goodId}")
    public int insertCart(@PathVariable("goodId")String goodId,HttpSession session){
        User user = (User) session.getAttribute("user");
        int uId = user.getuId();
        int num = 1;
        int cart = iCartService.insertCart(goodId, uId, num, date, date);
        return cart;
    }

    @RequestMapping("/delect/{goodId}")
    public int delectCart(@PathVariable("goodId")String goodId, HttpSession session){
        User user = (User) session.getAttribute("user");
        int uId = user.getuId();
        int cart = iCartService.delectCart(goodId,uId);
        return cart;
    }

    @RequestMapping("/updateAdd/{goodId}")
    public int addCart(@PathVariable("goodId")String goodId,HttpSession session){
        User user = (User) session.getAttribute("user");
        int uId = user.getuId();
        int cart = iCartService.addCart(goodId, uId);
        return cart;
    }

    @RequestMapping("/updateReduce/{goodId}")
    public int reduceCart(@PathVariable("goodId")String goodId,HttpSession session){
        User user = (User) session.getAttribute("user");
        int uId = user.getuId();
        int cart = iCartService.reduceCart(goodId, uId);
        return cart;
    }

    @RequestMapping("/updateCart/{goodId}/{num}")
    public int updateCart(@PathVariable("goodId")int goodId,@PathVariable("num") int num,HttpSession session){
        Date date=new Date();
        User user = (User) session.getAttribute("user");
        int uId = user.getuId();
        return iCartService.updateCart(num, date, goodId, uId);
    }
}
